import { AppType, ButtonProps } from "@/Util/Constant"
import * as PIXI from 'pixi.js'
import { PixiBase } from "./baseClass/PixiBase";
import { Broadcaster, DataChange, EventType } from "./broadcaster/Broadcaster";
import Hlddz from "./model/hlddz";
import { Button } from "./button/button";
import { SocketDataType, SocketEventType, SummaryObjType } from "@/share/commonType";
import socket from "@/socket/UserChatSocket";
import eventBus from "@/Util/EventBus";
import { renderText } from "@/Util";
let TextureCache = PIXI.utils.TextureCache

export class GameOver extends PixiBase {
    model: Hlddz = Hlddz.getInstance()
    buttons: PIXI.Container = new PIXI.Container();
    textContainer: PIXI.Container = new PIXI.Container();
    constructor(app: AppType) {
        super(app)
        this.addListener(this.model)
    }

    renderGameOverPanel(summaryObj: SummaryObjType) {
        this.textContainer.removeChildren()
        this.baseContainer.removeChildren()
        let myname = this.model.myname
        let summary = summaryObj[myname]
        // 创建遮罩层
        let mask = new PIXI.Graphics()
        mask.beginFill('#000', 0.7)
        mask.drawRect(0, 0, window.innerWidth, window.innerHeight)
        mask.endFill()
        this.baseContainer.addChild(mask)
        // 创建显示面板
        let bgSprite = new PIXI.Sprite(TextureCache[summary.isWin ? 'win' : 'lose'])
        bgSprite.width = 700 * this.scaleSize
        bgSprite.height = 600 * this.scaleSize
        bgSprite.position.set((window.innerWidth - bgSprite.width) / 2, (window.innerHeight - bgSprite.height) / 2)
        this.baseContainer.addChild(bgSprite)
        // 创建按钮
        this.baseContainer.addChild(this.buttons)
        let buttonConfig: ButtonProps[] = [
            { container: this.buttons, text: "退出游戏", bgColor: '#da3358', width: 150, handler: this.exitGame },
            { container: this.buttons, text: "再来一局", bgColor: '#68bceb', width: 150, handler: this.nextGame },
        ]
        buttonConfig.forEach((item, index) => {
            let props: ButtonProps = Object.assign(item, { posX: index * 200, scaleSize: this.scaleSize })
            new Button(props)
        })
        this.buttons.x = (window.innerWidth - this.buttons.width) / 2
        this.buttons.y = bgSprite.y + bgSprite.height - 100 * this.scaleSize
        // 渲染地主小图标
        let landSprite = new PIXI.Sprite(TextureCache['land-icon'])
        landSprite.scale.set(this.scaleSize)
        landSprite.x = -landSprite.width - 10 * this.scaleSize
        this.textContainer.addChild(landSprite)
        // 创建文字
        this.baseContainer.addChild(this.textContainer)
        this.textContainer.x = bgSprite.x + 150 * this.scaleSize
        this.textContainer.y = bgSprite.y + 250 * this.scaleSize
        let textLists = ['昵称', '底分', '倍数', '欢乐豆']
        textLists.forEach((text, index) => {
            renderText({
                text,
                container: this.textContainer,
                color: '#fff',
                posX: index * 120 * this.scaleSize,
                posY: 0,
                scaleSize: this.scaleSize,
            })
        })
        this.model.players.forEach(({ name }, index) => {
            let { nickname, lowScore, multiple, hld, isLandlord } = summaryObj[name]
            let renderTexts = [nickname, lowScore, multiple, hld]
            renderTexts.forEach((text, idx) => {
                isLandlord && (landSprite.y = (index * 60 + 50) * this.scaleSize)
                renderText({
                    text: text as string,
                    container: this.textContainer,
                    color: name === myname ? '#e5c07b' : '#fff',
                    posX: idx * 120 * this.scaleSize,
                    posY: (index + 1) * 60 * this.scaleSize,
                    scaleSize: this.scaleSize,
                })
            })
        })
    }

    exitGame = () => {
        let socketMsg = {
            action: SocketEventType.DdzGame,
            dataType: SocketDataType.QuitGame,
            data: {
                gameid: this.model.gameId,
                username: this.model.userinfo.username
            }
        }
        socket.emit(socketMsg)
        eventBus.emit('back-home')
    }

    nextGame = () => {
        this.baseContainer.removeChildren()
        this.model.gameReStart()
    }
    notify(caster: Broadcaster, event: EventType): void {
        let action = event.action
        switch (action) {
            case DataChange.GameOver:
                this.renderGameOverPanel(event.data)
                break;
            default:
                break;
        }
    }
}